﻿using System;
using System.Linq;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.DataLayer.Infra;
using ProjetoComperio.Framework.Extensions;

namespace ProjetoComperio.BusinessLogicLayer
{
    public class ApplicationBLL : ComperioDataObject
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="application"></param>
        public void Create(Application application)
        {
            DbContext.Applications.Add(application);
            DbContext.SaveChanges();
        }

        public void Edit(Application application)
        {
            Application original = this.RetrieveApplication(application.AcademicPersonID);
            original.CopyValuesFrom(application);
            DbContext.SaveChanges();
        }

        public Application RetrieveApplication(int ID)
        {
            return DbContext.Contributions.OfType<Application>().Where(Application => Application.ContributionID == ID).FirstOrDefault();
            
            //return DbContext.Applications.Where(p => p.AcademicPersonID == ID).First();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IQueryable<Application> RetrievApplications()
        {
            return DbContext.Applications;
        }


        public IQueryable<Application> RetrieveApplications(int academicPersonID)
        {
            return new ContributionBLL().RetrieveAcademicPersonContributions(academicPersonID).OfType<Application>();
            //return DbContext.Applications.Where(a => a.AcademicPersonID == academicPersonID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ID"></param>
        public void Remove(Int32 ID)
        {
            DbContext.Applications.Remove(RetrieveApplication(ID));
            DbContext.SaveChanges();
        }

    }
}